package com.leetcode.Offer;

import java.util.HashSet;

public class JZ56_I {
    public int[] singleNumbers(int[] nums) {
        //空间不符合要求
        int[] ints = new int[2];
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (set.contains(num)) {
                set.remove(num);
            } else {
                set.add(num);
            }
        }
        int a = 0;
        for (Integer integer : set) {

            ints[a++] = integer;
        }
        return ints;
    }

    public int[] singleNumbers2(int[] nums) {
        //剑指offer 275页
        int rs = 0;
        for (int num : nums) {
            rs ^= num;
        }
        int mask = 1;
        while ((rs & mask) == 0) {
            mask <<= 1;
        }
        int a = 0, b = 0;
        for (int num : nums) {
            if ((num & mask) == 0) {
                a ^= num;
            } else {
                b ^= num;
            }
        }
        return new int[]{a, b};
    }

}
